package com.example.test.advice.第十一章_开源世界.建议141_apache扩展包;

import org.apache.commons.beanutils.*;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.bidimap.TreeBidiMap;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.text.WordUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class Client {

    public static void main(String[] args) throws Exception {
        test();
        System.out.println("======================test2");
        test2();
        System.out.println("======================test3");
        test3();
        System.out.println("======================test4");
        test4();
        User person = new User();
        person.setName("张阿森纳");
        System.out.println(PropertyUtils.isReadable(person,"name"));
        System.out.println("======================test5");
        test5();
        System.out.println("======================test6");
        test6();
        System.out.println("======================test7");
        test7();
        System.out.println("======================test8");
        test8();
        System.out.println("======================test9");
        test9();
    }

    //字符串操作工具类
    private static void test(){
        //判断一个字符串是否为空，null或""都返回true
        String str = "Hello20";
        System.out.println("isEmptry:"+StringUtils.isEmpty(str));
        //判断是否为数字
        System.out.println("isNumeric:"+StringUtils.isNumeric(str));
        //最左边2个字符串
        System.out.println("left-2："+StringUtils.left(str,2));
        String string = "ll";
        //统计字符串出现的次数
        System.out.println("matches："+StringUtils.countMatches(str,string));
        //转义xml标示
        System.out.println("xml："+ StringEscapeUtils.escapeXml10(str));
        //随机生成长度为10的仅字母的字符串
        System.out.println("10个字母的字符串："+RandomStringUtils.randomAlphabetic(10));
        //随机生成长度为10的ascii字符串
        System.out.println("10个ascii："+RandomStringUtils.randomAscii(10));
        //以一个单词为操作对象，首字母大写，输出结果：Abc Bcd
        System.out.println("单词对象："+ WordUtils.capitalize("abc bcd"));
        System.out.println("单词对象："+ WordUtils.capitalize("abcbcd"));
    }

    //Object工具类
    public static void test2(){
        Person person = new Person();
        person.setAge(12);
        person.setName("张三");
        System.out.println(person);
    }

    //可变的包装类型
    public static void test3(){
        //声明一个可变的基本类型
        MutableInt mi = new MutableInt(10);
        //mi加10，结果为20
        mi.add(10);
        System.out.println(mi.intValue());
        mi.increment();
        //自加1，结果为21
        System.out.println(mi.intValue());
    }

    //使用属性复制时原始类必须是pulic的,commons下的BeanUtils性能可能比较差，建议使用Spring下面的BeanUtils
    public static void test4() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        User user = new User("张三",11);
        System.out.println("user:"+user);
        Person test = new Person();
        test.setName("赵六");
        test.setAge(3);
        Person person = new Person();
        BeanUtils.copyProperties(person,test);
        System.out.println("person:"+person);
       Person person2 = new Person();
        BeanUtils.copyProperties(person2,user);
        System.out.println("person2:"+person2);
        Person person3 = new Person();
        Map<String,String> map = new HashMap<>();
        map.put("name","王五");
        BeanUtils.copyProperties(person3,map);
        System.out.println("person3:"+person3);
    }

    //动态Bean和自由Bean
    private static void test5() throws IllegalAccessException, InstantiationException {
        //动态Bean,首先定义Bean类
        DynaProperty[] props = new DynaProperty[]{
                new DynaProperty("name",String.class),
                new DynaProperty("age",int.class)
        };
        BasicDynaClass dynaClass = new BasicDynaClass("people",null,props);
        //动态Bean对象
        DynaBean person = dynaClass.newInstance();
        person.set("name","张三");
        System.out.println(person.get("name"));
        //自由Bean
        DynaBean user = new LazyDynaBean();
        //直接定义属性和值
        user.set("name","张三");
        //定义属性名，限定属性类型为Map
        user.set("phoneNum","tel","021");
        user.set("phoneNum","mobile","138");
        //属性类型为ArrayList
        user.set("address",0,"上海");
        user.set("address",1,"北京");
        System.out.println("自由Bean:"+user.get("address"));
    }

    //转换器
    private static void test6() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        //一个简单的Bean对象
        User user = new User("张三",18);
        //转换工具
        ConvertUtilsBean cub = new ConvertUtilsBean();
        //注册一个转换器
        cub.register(new Converter() {
            @Override
            public Object convert(Class aClass, Object o) {
                //为每个String属性加前缀
                return "prefix-"+o;
            }
        },String.class);
        //建立一个依赖特定转换工具的Bean工具类
        BeanUtilsBean beanUtils = new BeanUtilsBean(cub);
        //输出结果为：prefix-张三
        String result = beanUtils.getProperty(user,"name");
        System.out.println("result:"+result);
    }

    //Bag--提供了重复元素的统计功能
    private static void test7(){
        Bag box = new HashBag(Arrays.asList("red","blue","black","blue"));
        //又增加了3个蓝球
        box.add("blue",3);
        System.out.println("蓝色球的数量："+box.getCount("blue"));
    }

    //lazy系列，在我需要你的时候，你再出现
    private static void test8(){
        //把list包装成一个lazy类型
        List<String> lazy = LazyList.decorate(new ArrayList(), new Factory() {
            @Override
            public String create() {
                return "A";
            }
        });
        //访问了第四个元素，此时0、1、2、元素为Null
        String obj = lazy.get(3);//第一次访问哪个下标，初始化的元素就在哪个下标下
        //追加一个元素
        lazy.add("第五个元素");
        System.out.println("lazy.size:"+lazy.size()+",lazy:"+lazy);
    }

    //双向Map
    private static void test9(){
        //key、value都不允许重复的map
        BidiMap bidiMap = new TreeBidiMap();
        bidiMap.put(1,"壹");
        //bidiMap.put(2,"壹");//如果出现重复的value，那么会覆盖
        //根据key获取value
        System.out.println("get:"+bidiMap.get(1));
        //根据值获取key
        System.out.println("getKey:"+bidiMap.getKey("壹"));
        //根据Value删除键值对
        bidiMap.removeValue("壹");
        System.out.println("bidiMap:"+bidiMap);
    }
}

